<?php

/** 
 * @property App_version_mdl $version
 * @property ApiOutput $apiOutput
 * @property Browser $browser
 * @property StudentsMzxyModel $StudentsMzxyModel
 */
class App extends MY_Controller {
	public function __construct() {
		parent::__construct ();
		$this->load->model('App_version_mdl','version');
	}
	/**
	 * 获取APP配置参数
	 * 	传入参数: 
	 * 		os:系统(ios/android)
	 * 		v_app:app内部版本号
	 * 		v_tpl:模板数据版本号
	 * 		v_base:基础数据版本号
	 * 		v_ext:个人中心扩展项版本号
	 * 		v_school:学校数据版本号
	 */
	public function setting(){
		$system  = $this->input->get_post('os',TRUE);
		if(  ! in_array($system, array('ios','android')) ){
			$this->apiOutput->errorInput();
		}
		
		$user = parent::getLoginUser(FALSE);
		$uid  = empty($user) ? 0 : $user->id;
		
		$appVersion = (int) $this->input->post_get('v_app');
		$tplVersion = (int) $this->input->post_get('v_tpl');
		$baseVersion = (int) $this->input->post_get('v_base');
		$extVersion = (int) $this->input->post_get('v_ext');
		$schoolVersion = (int) $this->input->post_get('v_school');
		$rechargeVersion = (int)$this->input->post_get('v_recharge');
		
		$appVersion = $appVersion > 0 ? $appVersion : 0;
		$tplVersion = $tplVersion > 0 ? $tplVersion : 0;
		$baseVersion = $baseVersion > 0 ? $baseVersion : 0;
		$extVersion = $extVersion > 0 ? $extVersion : 0;
		$schoolVersion = $schoolVersion > 0 ? $schoolVersion : 0;
		$rechargeVersion = $rechargeVersion > 0 ? $rechargeVersion : 0;
		
		$out = array(
				'enableWxLogin'=>0,// 启用微信登录
				'app'=>array('hasNew'=>0),
				'tpl'=>array('hasNew'=>0),
				'base'=>array('hasNew'=>0),
				'ext'=>array('hasNew'=>0),
				'school'=>array('hasNew'=>0),
				'recharge'=>array('hasNew'=>0),
		);
		
		
		$lastAppVersion = $this->version->getLastVersion($system);
		if ( $appVersion < $lastAppVersion['internal_version'] ) {
			$out['app']['hasNew'] = 1;
			$out['app']['version'] = $lastAppVersion['internal_version'];
			$out['app']['data']   = $lastAppVersion;
		}
		if( $tplVersion < 1 ){
			$out['tpl']['hasNew'] = 1;
			$out['tpl']['version'] = 1;
			$out['tpl']['data'] = $this->getTplData();
		}
		if( $baseVersion < 2 ){
			$out['base']['hasNew'] = 1;
			$out['base']['version'] = 2;
			$out['base']['data']   = array(
					'buyCycle'=>1,//买水通知周期
					'devPrefix'=> array('RJ','runjoy','USR','RX','RL'),//蓝牙设备前缀
					'unAcessBtFreeTime'=>120,//未授权蓝牙设备空闲时间
			);
		}
		if( $rechargeVersion < 1 ){
			$out['recharge']['hasNew'] = 1;
			$out['recharge']['version'] = 1;
			$out['recharge']['data']   = array(
					'items'=>array('10','30','50','80','100'),
			);  
		}
		$extCurrVersion = time();
		if( $extVersion < $extCurrVersion ){
			$out['ext']['hasNew'] = 1;
			$out['ext']['version'] = $extCurrVersion;
			$out['ext']['data'] = array(
 					//array (
 					//		'x2' => 'https://heshui-api.yxlady.com/appicons/2id@2x.png',
 					//		'x3' => 'https://heshui-api.yxlady.com/appicons/2id@3x.png',
 					//		't' => '高级护肤师一对一教你祛痘美白！',
 					//		'url' => 'http://tj.xantravel.com/v4/heshui0420.shtml?v='.time(), 
 					//		'o'=>"1",
 					//),
					array (
							'x2' => 'https://heshui-api.yxlady.com/appicons/wenjuan@2x.png',
							'x3' => 'https://heshui-api.yxlady.com/appicons/wenjuan@3x.png',
							't' => '荣钧饮水问卷调查',
							'url' => base_url('wj/index?v='.time().'&uid='.$uid),
							'o'=>"0",
					),
			);
// 			if(!empty($user)){
// 				$this->load->model('StudentsMzxyModel');
// 				if($this->StudentsMzxyModel->checkSchool($user)){
// 					$out['ext']['data'][] = array(
// 							'x2' => 'https://heshui-api.yxlady.com/appicons/wenjuan@2x.png',
// 							'x3' => 'https://heshui-api.yxlady.com/appicons/wenjuan@3x.png',
// 							't' => '民族学院一卡通绑定',
// 							'url' => base_url('mzxy/index?'.COOKIE_KEY_AUTH.'='.$user->getAuthInfo()),
// 							'o'=>"0",
// 					);
// 				}
// 			}
			
			
			
		}
		
		$this->load->model('School_mdl','school');
		$schoolList = $this->school->getList('id,name');
		
		$hiden = array(2,8);
		$schoolNew = array();
		
		if ( !empty($schoolList) ){
			foreach ($schoolList as $k=>$v){
				if (!in_array($v['id'], $hiden) ){
					$schoolNew[] = $v;
				}
			}
		}
		if( $schoolVersion < 1 ){
			$out['school']['hasNew'] = 1;
			$out['school']['version'] = 1;
// 			$out['school']['data'] = array(
// 				array('id'=>1,'name'=>'湖南信息学院'),
// // 				array('id'=>2,'name'=>'伊秀测试'),
// 				array('id'=>3,'name'=>'湖南民族职业学院'),
// 			);
			$out['school']['data'] = $schoolNew;
		}
		//推广
		/*
		$out['tg'] = array(
						'banner'=>'https://miniapp-api.yxlady.com/fram/upload/banner_rj.jpg',
						'qc'=>'https://gift-static.yxlady.com/fram_1/share/show_ask_img/1141188_3.jpg'
		);
		*/
		$out['tg'] = array(
		    'banner'=>'',
		    'qc'=>'https://gift-static.yxlady.com/fram_1/share/show_ask_img/1141188_3.jpg'
		);
		
		$this->apiOutput->out($out);
	}
	
	private function getTplData(){
		$usrIconsDir = base_url('appicons/usr').'/';
		$devIconsDir = base_url('appicons/dev').'/';
		$tpl = array(
				"version"=>1,
				"usr"=>array(
						'pageTitle'=>'个人中心',
						'notboundWx'=>get_tpl_item('未绑定微信',$usrIconsDir.'wx'),
						'boundWx'=>get_tpl_item('已绑定微信',$usrIconsDir.'wx'),
						'notboundPhone'=>get_tpl_item('未绑定手机',$usrIconsDir.'phone'),
						'boundPhone'=>get_tpl_item('已绑定手机',$usrIconsDir.'phone'),
						'wallet'=>get_tpl_item('我的钱包',$usrIconsDir.'wallet'),
						'order'=>get_tpl_item('我的订单',$usrIconsDir.'order'),
						'offline'=>get_tpl_item('离线取水',$usrIconsDir.'offline'),
						'about'=>get_tpl_item('关于我们',$usrIconsDir.'guanyu'),
						'logout'=>get_tpl_item('退出登录',$usrIconsDir.'logout'),
				),
				"devList"=>array(
						'pageTitle'=>'饮水系统',
						'xyj'=>array(
								't'=>'设备名:{{devName}}',
								'sub'=>'设备别名:{{alias}}',
								'x2'=>$devIconsDir.'xiyiji@2x.png',
								'x3'=>$devIconsDir.'xiyiji@3x.png',
						),
						'ysj'=>array(
								't'=>'设备名:{{devName}}',
								'sub'=>'设备别名:{{alias}}',
								'x2'=>$devIconsDir.'yinshui@2x.png',
								'x3'=>$devIconsDir.'yinshui@3x.png',
						),
				),
// 				"myWallet"=>array(
// 						'pageTitle'=>'我的钱包',
// 						'header'=>array(),
// 				),
				"orderList" => array(
						"pageTitle"=>'我的订单',
						"onlineTitle"=>'在线取水{{takeSec}}秒',
						"offlineTitle"=>'离线取水{{takeSec}}秒',
				),
		);
		
		
		return $tpl;
	}
	
	public function phone_info(){
		$phoneID = $this->input->post_get('phoneID',TRUE);
		$os      = $this->input->post_get('os',TRUE);
		$model   = $this->input->post_get('model',TRUE);
		
		$user    = $this->getLoginUser(FALSE);
		
		if ( empty($user) ) {
			$this->apiOutput->error('用户未登录');
		}
		if( empty($phoneID) || empty($os) || empty($model) ){
			$this->apiOutput->errorInput();
		}
		
		$this->load->model('User_phone_mdl','userPhone');
		
		$where = array(
				'userID'=>$user->id,
				'phoneID'=>$phoneID,
		);
		
		$info = $this->userPhone->getOne($where);
		if( empty($info) ){
			$insert = array(
					'userID'=>$user->id,
					'phoneID'=>$phoneID,
					'os'=>$os,
					'model'=>$model,
			);
			
			$res = $this->userPhone->insert($insert);
			if( ! $res ){
				$this->apiOutput->errorSystem();
			}
			
		}
		
		$this->apiOutput->out();
	}
	
	
	
	
	/**
	 * 检查版本API
	 * 传入参数：内部版本号
	 */
	public function check_android(){
		$input = check_input($_REQUEST,array('version'));
		if( !$input ){
			$this->apiOutput->errorInput();
		}
		$internal_version = $input['version'];
	
		$lastVersion = $this->version->getLastVersion('android');
	
		if( $internal_version < $lastVersion['internal_version'] ){
			unset($lastVersion['id']);
			unset($lastVersion['upload_time']);
			$this->apiOutput->put('status', 1)
							->put('data',$lastVersion)
							->out();
		}else{
			$this->apiOutput->put('status', 0)->msg('您当前已经是最新版本！');
		}
	
	}
	
	public function check_ios(){
		$version = (int)$this->input->post_get('version');
		if (empty($version)) {
			$this->apiOutput->errorInput();
		}
		$lastVersion = $this->version->getLastVersion('ios');
		if( $version < $lastVersion['internal_version'] ){
			$this->apiOutput->put('status', 0)
							->put('version', $lastVersion)
							->put('msg', '抱歉,当前版本已停止使用,请到AppStore更新最新版本!')
							->out();
		}else{
			$this->apiOutput->put('status', 1)->out();
		}
	}
	
	public function download(){
		$this->load->library('Browser',NULL,'browser');
		$system = $this->browser->isIOS ? 'ios' : 'android';
		$temp   = array();
		$temp['system'] = $system;
		
		if ( $this->browser->isWechat ) {
			$this->load->view('download/wechat',$temp);
			return true;
		}
		if( $system=='ios' ){
			header("Location:https://itunes.apple.com/gb/app/yi-dong-cai-bian/id1146090418?mt=8");
			exit;
		}
		$version = $this->version->getLastVersion($system);
		
		$temp['version'] = $version;
		$this->load->view('download/index',$temp);
	}
	
	public function about(){
		$this->load->view('app/about');
	}
	
	public function price(){
		echo "<h1>价格表</h1>";
// 		$this->load->model('Water_price_mdl','price');
// 		$prices['normal'] = $this->price->getPrice(BUY_MODE_NORMAL);
// 		$prices['once']   = $this->price->getPrice(BUY_MODE_ONCE);
// 		$temp['prices'] = $prices;
// 		$this->load->view('app/price',$temp);
	}
	
	public function check_msg(){
		$out = array('hasNew'=>0);  
//  		$out = array (
//  				'hasNew' => 1,
//  				'count' => 1,
//  				'url' => 'http://tj.xantravel.com/v2/wxqz_lwqyx_2x.shtml',
// // // 				'url' => base_url ( 'app/message' ),
//  				'x2' => base_url ( 'appicons/xiaoxi@2x.png' ),
//  				'x3' => base_url ( 'appicons/xiaoxi@3x.png' ) 
//  		);
		$this->apiOutput->out ( $out );
	}
	
	public function message(){
		echo  '<center><h1>欢迎使用 荣钧饮水</h1></center>';
	}
	
	
	public function check_recharge(){
		
		$user = parent::getLoginUser();
		
		$isFirst = false;//是否首次充值
		
		//判断是否首充
		$this->load->model('Recharge_order_mdl','recharge_order');
		
		//每年首充日期
		$nowYear = date('Y',time());
		$openDate = $nowYear.'-08-01';
		$openTime = strtotime($openDate);
		
// 		$isRecharge = $this->recharge_order->getOne(array('userID'=>$user->id,'wxTradeType'=>'APP','tradeState'=>'SUCCESS','payTime >'=>$openTime),'id');
		
		$price = 50;
		
// 		if ( empty($isRecharge) ){
// 			$isFirst = true;
// 			//查询首充金额
// 			if ( !empty($user->schoolID) ){
// 				$this->load->model('school_mdl','school');
// 				$school = $this->school->getOneById($user->schoolID);
// 				if (!empty($school['firstChargePrice'])){
// 					$price = $school['firstChargePrice'];
// 				}
// 			}
			
// 		}
		
		$recharge = array('isFirst'=>$isFirst,'price'=>$price);
	
		$this->apiOutput->put('recharge',$recharge)->out();
	
	}
	
	
	
}

function get_tpl_item($title,$iconBaseUrl='',$ext='png'){
	$item = array('t'=>$title);
	if( ! empty($iconBaseUrl) ){
		$item['x2'] = $iconBaseUrl.'@2x.'.$ext;
		$item['x3'] = $iconBaseUrl.'@3x.'.$ext;
	}
	return $item;
}


